package com.kedll.library;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kedll.library.util.CollectionUtils;
import com.kedll.library.util.MyUtils;

/**
 * @author QiQi
 */
public abstract class KNavigateActivity extends KBaseActivity {

    public final static String ACTION_SHOW_ENTRY_BUTTON = "show_entry_btn";
    protected ViewPager mPager;
    protected MyAdapter adapter;
    protected View[] point;
    protected View[] mImageViews;
    protected ImageView movePoint;
    protected TextView btn_entry;
    boolean showBtn;
    private int distance;
    private LinearLayout layout_point;
    private ViewPager.OnPageChangeListener onPageChangeListener;
    private int navigateLength;

    private int selectPoint = R.drawable.circle_point1;
    private int unSelectPoint = R.drawable.circle_point0;


    public void setSelectPoint(int selectPoint) {
        this.selectPoint = selectPoint;
    }

    public void setUnSelectPoint(int unSelectPoint) {
        this.unSelectPoint = unSelectPoint;
    }

    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }


    public int getNavigateLength() {
        return navigateLength;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_navigate;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        layout_point = findViewById(R.id.layout_navigate_point);
        btn_entry = findViewById(R.id.enter_app_btn);
        btn_entry.setVisibility(View.GONE);
        btn_entry.setOnClickListener(v -> {
            showStartActivity();
            finish();
        });

        Intent data = getIntent();
        showBtn = data.getBooleanExtra(ACTION_SHOW_ENTRY_BUTTON, true);

        movePoint = new ImageView(this);
        movePoint.setImageResource(selectPoint);
        movePoint.setVisibility(View.VISIBLE);
        ViewGroup rootView = (ViewGroup) getWindow().getDecorView();
        rootView.addView(movePoint);

        mPager = findViewById(R.id.navigate_pager);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int margin = MyUtils.dp2px(5);
        params.setMargins(margin, margin, margin, margin);

        int[] imgArray = getImageArray();
        if (imgArray == null || imgArray.length == 0) {
            throw new NullPointerException("Navigate Image must not be null");
        }
        point = new ImageView[imgArray.length];
        mImageViews = new View[imgArray.length];
        navigateLength = CollectionUtils.size(point);

        for (int i = 0; i < point.length; i++) {
            ImageView img = new ImageView(this);
            img.setId(0x7000 + i);
            img.setImageResource(unSelectPoint);
            point[i] = img;
            layout_point.addView(img, params);
        }
        for (int i = 0; i < mImageViews.length; i++) {
//            ImageView imageView = new ImageView(this);
//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            mImageViews[i] = imageView;
//            imageView.setImageResource(imgArray[i]);
            View view = LayoutInflater.from(this).inflate(imgArray[i], null);
            mImageViews[i] = view;
        }

        adapter = new MyAdapter();
        mPager.setAdapter(adapter);
        mPager.setOnPageChangeListener(new PagerListener());
    }

    @Override
    protected void initEvent() {
        findViewById(R.id.bt_jump).setOnClickListener(v -> {

            showStartActivity();
            finish();
        });
    }

    /**
     * 跳转到首页
     */
    public abstract void showStartActivity();

    /**
     * 获取引导图数组
     *
     * @return
     */
    public abstract int[] getImageArray();

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        //
        int[] loc = new int[2];
        point[0].getLocationOnScreen(loc);
        movePoint.setX(loc[0]);
        movePoint.setY(loc[1]);
        ViewGroup.LayoutParams params = movePoint.getLayoutParams();
        params.width = point[0].getWidth();
        params.height = point[0].getHeight();
        movePoint.setLayoutParams(params);

        int[] lt = new int[2];
        point[1].getLocationOnScreen(lt);
        distance = lt[0] - loc[0];
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class PagerListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // 滑动的时候调用
            int cre = (int) (distance * arg1);
            int[] loc = new int[2];
            point[arg0].getLocationOnScreen(loc);
            int xCoordinates = loc[0] + cre;
            movePoint.setX(xCoordinates);
            if (onPageChangeListener != null) {
                onPageChangeListener.onPageScrolled(arg0, arg1, arg2);
            }
        }

        @Override
        public void onPageSelected(int position) {
            int[] loc = new int[2];
            point[position].getLocationOnScreen(loc);
            movePoint.setX(loc[0]);

            if (position == (mImageViews.length - 1) && showBtn) {
                layout_point.setVisibility(View.GONE);
                movePoint.setVisibility(View.GONE);
                btn_entry.setVisibility(View.VISIBLE);
                btn_entry.startAnimation(AnimationUtils.loadAnimation(KNavigateActivity.this, R.anim.scal_anim));
            } else {
                btn_entry.setVisibility(View.GONE);
                movePoint.setVisibility(View.VISIBLE);
                layout_point.setVisibility(View.VISIBLE);
            }
            if (onPageChangeListener != null) {
                onPageChangeListener.onPageSelected(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int position) {
            //
            if (onPageChangeListener != null) {
                onPageChangeListener.onPageScrollStateChanged(position);
            }
        }
    }

    private class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageViews.length;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(mImageViews[position % mImageViews.length], 0);
            return mImageViews[position % mImageViews.length];
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(mImageViews[position % mImageViews.length]);
        }

        @Override
        public boolean isViewFromObject(@NonNull View arg0, @NonNull Object arg1) {
            return arg0 == arg1;
        }

    }

}